package dao.reader.impl;

import dao.BaseDao;
import dao.reader.BooksDao;
import entity.Books;

import java.awt.print.Book;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * BooksImpl
 *
 * @author lenovo
 * @since 2023/8/30
 */
public class BooksImpl extends BaseDao implements BooksDao {

    @Override
    public List<Books> getList(String condition, String conditionValue, int pageIndex, int pageSize) {
        String sql = "select * from books ";
        if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")){
            sql += " where " + condition + " like concat('%',?,'%')";
        }
        sql += " limit ?,?";
        List<Books> list = new ArrayList<>();
        Books books = null;
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            int num = 0;
            if (condition != null && conditionValue != null){
                pstmt.setString(++num,conditionValue);
            }
            pstmt.setInt(++num,pageIndex);
            pstmt.setInt(++num,pageSize);
            rs = pstmt.executeQuery();
            while (rs.next()){
                books = new Books();
                books.setId(rs.getInt("id"));
                books.setName(rs.getString("name"));
                books.setAuthor(rs.getString("author"));
                books.setLibrary_id(rs.getInt("library_id"));
                books.setSort_id(rs.getInt("sort_id"));
                books.setPosition(rs.getString("position"));
                books.setStatus(rs.getInt("status"));
                books.setDescription(rs.getString("description"));
                list.add(books);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(rs,pstmt,conn);
        }
        return list;
    }

    @Override
    public int pageCount(String condition, String conditionValue) {
        String sql = "select COUNT(*) c from books";
        if (condition != null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")){
            sql += " where " + condition + " like concat('%',?,'%')";
        }
        int total = 0;
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            if (condition != null && conditionValue != null){
                pstmt.setString(1,conditionValue);
            }
            rs = pstmt.executeQuery();
            if (rs.next()){
                total = rs.getInt("c");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(rs,pstmt,conn);
        }
        return total;
    }
}
